<?php $this->pageHeader('Hovedside'); ?>

<?php /*
<h1>Velkommen til Trondheim Røde Kors Hjelpekorps</h1>
<p>Disse sidene inneholder først og fremst intern informasjon for medlemmene i Trondheim Røde Kors Hjelpekorps. Som gjest er du allikevel velkommen til å ta en kikk på de delene som ikke krever innlogging.</p>
<p>&nbsp;</p>
<p>Dersom du ønsker å søke opptak i Hjelpekorpset, eller bare er nysgjerrig på hva vi driver med, kan du følge linken til våre <a href="http://www.rodekors.no/trondheim">åpne infosider</a>.</p>
*/ ?>

<?php echo $this->identity; ?>

<?php echo $this->render('index/calendar.phtml'); ?>

<?php if (!is_null($this->shiftExchanges)): ?>
    <h2>Ubehandlede forespørsler om vaktbytte</h2>
<?php endif; ?>
<?php if (sizeof($this->shiftExchanges) > 0): ?>
    <table class="core_index_shiftexchanges">
        <thead>
            <tr>
                <th>Arrangement</th>
                <th>Vakt</th>
                <th>Rolle</th>
                <th>Fra</th>
                <th>Til</th>
                <th class="buttons">&nbsp;</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($this->shiftExchanges as $shiftExchange): ?>
                <tr>
                    <td><a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'event', 'action' => 'view', 'id' => $shiftExchange->ShiftRole->Shift->Event->id)); ?>"><?php echo $shiftExchange->ShiftRole->Shift->Event->name; ?></a></td>
                    <td><a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'view', 'id' => $shiftExchange->ShiftRole->Shift->id)); ?>"><?php echo $shiftExchange->ShiftRole->Shift->name; ?></a></td>
                    <td><?php echo $shiftExchange->ShiftRole->Role->name; ?></td>
                    <td><a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'user', 'action' => 'view', 'id' => $shiftExchange->SourceMember->User->id)); ?>"><?php echo $shiftExchange->SourceMember->first_name . ' ' . $shiftExchange->SourceMember->last_name; ?></a></td>
                    <td><a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'user', 'action' => 'view', 'id' => $shiftExchange->TargetMember->User->id)); ?>"><?php echo $shiftExchange->TargetMember->first_name . ' ' . $shiftExchange->TargetMember->last_name; ?></a></td>
                    <td class="buttons"><a href="<?php echo $this->url(array('module' => 'admin', 'controller' => 'shift-exchange', 'action' => 'accept', 'id' => $shiftExchange->id)); ?>"><img src="<?php echo $this->imageLink('images/icons/accept.png'); ?>" title="Godkjenn" /></a> <a href="<?php echo $this->url(array('module' => 'admin', 'controller' => 'shift-exchange', 'action' => 'reject', 'id' => $shiftExchange->id)); ?>"><img src="<?php echo $this->imageLink('images/icons/cancel.png'); ?>" title="Avsl&aring;" /></a></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <p>&nbsp;</p>
<?php elseif (!is_null($this->shiftExchanges)): ?>
    <p>Det er ingen ubehandlede vaktbytteforespørsler.</p>
    <p>&nbsp;</p>
<?php endif; ?>

<?php if (!is_null($this->unconfirmedUsers)): ?>
    <h2>Ubekreftede brukere</h2>
<?php endif; ?>
<?php if (sizeof($this->unconfirmedUsers) > 0): ?>
    <table class="core_index_unconfirmed_users">
        <thead>
            <th>Navn</th>
            <th>Brukernavn</th>
            <th class="buttons">&nbsp;</th>
        </thead>
        <tbody>
        <?php foreach($this->unconfirmedUsers as $user):
        $memberName = $this->escape($user['Member']['first_name'] . ' ' . $user['Member']['last_name']); ?>
            <tr>
                <td><a href="<?php echo $this->url(array('module' => 'admin', 'controller' => 'member', 'action' => 'edit', 'id' => $user['member_id']), null, true); ?>"><?php echo $memberName; ?></a></td>
                <td><?php echo $this->escape($user['username']); ?></td>
                <td class="buttons">
                    <?php if($user['is_confirmed'] == '0'): ?>
                        <a href="<?php echo $this->url(array('module' => 'admin', 'controller' => 'user', 'action' => 'confirm', 'id' => $user['id']), null, true); ?>"><img src="<?php echo $this->imageLink('images/icons/accept.png'); ?>" alt="Aktiver <?php echo $this->escape($user['username']); ?>" title="Bekreft" /></a>
                    <?php endif; ?>
                </td>
            </tr>
        <?php endforeach;?>
        </tbody>
    </table>
    <p>&nbsp;</p>
<?php elseif (!is_null($this->unconfirmedUsers)): ?>
    <p>Det er ingen ubekreftede brukere</p>
    <p>&nbsp;</p>
<?php endif; ?>

<h2>Kommende vakter</h2>
<?php if (count($this->futureShifts) > 0) : ?>
    <table>
        <thead>
            <tr>
                <th>Vakt</th>
                <th>Oppmøte depot</th>
                <th>Oppmøte arr</th>
                <th>Antatt ferdig</th>
                <th>Arrangement</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach($this->futureShifts as $shift) : ?>
            <tr>
                <td>
                    <a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'view', 'id' => $shift['id']), null, true); ?>">
                        <?php echo $this->escape($shift['name']); ?>
                    </a>
                </td>
                <?php
                    $appearanceTime = new RedCross_Date();
                    $appearanceTime->import($shift['appearance_time']);
                    
                    $fromDate = new RedCross_Date();
                    $fromDate->import($shift['from_date']);

                    $toDate = new RedCross_Date();
                    $toDate->import($shift['to_date']);
                ?>
                <td><?php if ($appearanceTime == $fromDate) { echo "Direkte på arr"; } else { echo $this->escape($appearanceTime->getDateTimeStr()); } ?></td>
                <td><?php echo $this->escape($fromDate->getDateTimeStr()); ?></td>
                <td><?php echo $this->escape($toDate->getDateTimeStr()); ?></td>
                <td>
                    <a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'event', 'action' => 'view', 'id' => $shift['Event']['id']), null, true); ?>">
                        <?php echo $this->escape($shift['Event']['name']); ?>
                    </a>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <p>&nbsp;</p>
<?php else : ?>
  Ingen kommende vakter
  <p>&nbsp;</p>
<?php endif; ?>

<h2>Ubekreftede vaktutkallinger</h2>
<?php if (sizeof($this->unconfirmedShifts) > 0): ?>
    <table class="core_index_unconfirmed_shifts">
        <thead>
            <th>Vakt</th>
            <th>Oppmøte</th>
            <th>Rolle</th>
            <th>&nbsp;</th>
        </thead>
        <tbody>
        <?php foreach ($this->unconfirmedShifts as $shift):
            $shiftName = '<a href="' . $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'view', 'id' => $shift->id), null, true) . '">' . $this->escape($shift->name) . '</a>';

            $appearanceTime = new RedCross_Date();
            $appearanceTime->import($shift->appearance_time);
            $appearanceTime = $appearanceTime->getDateTimeStr();


            $rolesOnShift = $shift->getMemberRoles($this->authInfo()->getMemberId());
            $multipleRoles = count($rolesOnShift) > 1;
        ?>
            <tr>
                <td><?php echo $shiftName; ?></td>
                <td><?php echo $appearanceTime; ?></td>
                <td>
                    <?php if ($multipleRoles): ?>
                        <ul>
                        <?php foreach ($rolesOnShift as $role): ?>
                            <?php if (is_null($role->notification_confirmed_time)): ?>
                                <li><?php echo $role->Role->name; ?></li>
                            <?php endif; ?>
                        <?php endforeach; ?>
                        </ul>
                    <?php else: ?>
                        <?php echo $rolesOnShift[0]->Role->name; ?>
                    <?php endif; ?>
                </td>
                <td>
                    <?php if ($multipleRoles): ?>
                        <ul>
                        <?php foreach ($rolesOnShift as $role): ?>
                            <?php if (is_null($role->notification_confirmed_time)): ?>
                                <li>
                                    <a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'confirm', 'id' => $role->id, 'key' => RedCross_Global::shift_role_hash($role)), null, true); ?>">Bekreft</a>
                                </li>
                            <?php endif; ?>
                        <?php endforeach; ?>
                        </ul>
                    <?php else: ?>
                        <a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'confirm', 'id' => $rolesOnShift[0]->id, 'key' => RedCross_Global::shift_role_hash($rolesOnShift[0])), null, true); ?>">Bekreft</a>
                    <?php endif; ?>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <p>&nbsp;</p>
<?php else: ?>
    <p>Du har ingen ubekreftede vakter.</p>
    <p>&nbsp;</p>
<?php endif; ?>

<h2>Vaktrapporter</h2>
<?php 
$noMissingReports = true;

if (sizeof($this->missingshiftreports) > 0): 
    $first = true;

    foreach ($this->missingshiftreports as $shift):
        $rolesOnShift = $shift->getMemberRoles($this->authInfo()->getMemberId());

        $isLeaderOnShift = false;
        if ($rolesOnShift):
            $multipleRoles = count($rolesOnShift) > 1;
            foreach ($rolesOnShift as $role):
                if ((bool) $role->leader):
                    $isLeaderOnShift = true;
                    break;
                endif;
            endforeach;
        endif;

        $isShiftReportApproved = (!is_null($shift->shift_report_id) && (bool)$shift->ShiftReport->approved);

        if (!$isShiftReportApproved && $isLeaderOnShift) :
            $noMissingReports = false;
            
            $shiftName = '<a href="' . $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'view', 'id' => $shift->id), null, true) . '">' . $this->escape($shift->name) . '</a>';
            $shiftReportOperation = '';
            
            if (is_null($shift->shift_report_id)) :
                $shiftReportOperation = 'Lag vaktrapport';
            else :
                $shiftReportOperation = 'Endre vaktrapport';
            endif;

            $shiftReport = '<a href="' . $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'report', 'id' => $shift->id), null, true). '">'.$shiftReportOperation.'</a>';

            if ($first) :
                $first = false;
?>
    <table class="core_index_missing_shiftreports">
        <thead>
            <th>Vakt</th>
            <th>&nbsp;</th>
        </thead>
        <tbody>
            <?php endif;
            ?>
            <tr>
                <td><?php echo $shiftName; ?></td>
                <td><?php echo $shiftReport; ?></td>
            </tr>
        <?php endif;
        
    endforeach; 
        ?>
        </tbody>
    </table>
<?php endif;
if ($noMissingReports):
 ?>
    <p>Du har ingen manglende vaktrapporter.</p>
<?php endif; ?>
<p>&nbsp;</p>

<h2>Kurs som utgår om mindre enn én måned</h2>
<?php if (sizeof($this->expiringCourses) > 0): ?>
    <table class="core_index_expiring_courses">
        <thead>
            <th>Kurs</th>
            <th>Gyldig til</th>
        </thead>
        <tbody>
        <?php foreach($this->expiringCourses as $course):
            $toDate = new RedCross_Date();
            $toDate->import($course['MemberCourse']['0']['expires_date']);
            $toDate = $this->escape($toDate->getDateTimeStr());
        ?>
            <tr>
                <td><?php echo $this->escape($course['name']); ?></td>
                <td><?php echo $this->escape($toDate); ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <p>&nbsp;</p>
<?php else: ?>
    <p>Du har ingen kurs som utgår om mindre enn én måned.</p>
<?php endif; ?>

